<?php 
class DomArOneTestCase extends DomArTestUnitTestCase { 
	
	public function testConstruct() {
		$a = new DomArOneTestClass('a');
		$c = new DomArOneChildTestClass('c');
		
		$prop = $a->getProperty('aggrChild');
		$this->assertIdentical( $prop->childClassName, 'DomArOneChildTestClass');
		$this->assertIdentical( $prop->className, 'DomArOneTestClass' );
		$this->assertIdentical( $prop->inverse, NULL );
		$this->assertIdentical( $prop->actAsParent, true );
		$this->assertIdentical( $prop->composition, false );
		
		$prop = $a->getProperty('child');
		$this->assertIdentical( $prop->childClassName, 'DomArOneChildTestClass');
		$this->assertIdentical( $prop->className, 'DomArOneTestClass' );
		$this->assertIdentical( $prop->inverse, NULL );
		$this->assertIdentical( $prop->actAsParent, true );
		$this->assertIdentical( $prop->composition, true );	
	}

	public function testSet() {
		
		DomArOneTestClass::getTable()->update();
		
		$a1 = new DomArOneTestClass('a');
		$a2 = new DomArOneTestClass('a');
		$b = new DomArOneChildTestClass('b');
		$c = new DomArOneChildTestClass('c');
		$d = new DomArOneChildTestClass('d');
		$e = new DomArOneChildTestClass('e');
		$f = new DomArOneChildTestClass('f');
		$g = new DomArOneChildTestClass('g');
		

		$a1->child = $b;
		$this->assertIdentical( $a1->child, $b );


		$a1->save();
		$this->assertIdentical( $a1->child, $b );
		$this->assertObjectDatabaseValue( $a1, 'child', $b->id );
		$this->assertObjectExist( $a1 );
		$this->assertObjectExist( $b );
		
		$a1->child = $d;
		$this->assertIdentical( $a1->child, $d );
		$this->assertObjectDatabaseValue( $a1, 'child', NULL );
		$this->assertObjectExist( $a1 );
		$this->assertObjectNotExist( $b );
		
		$a1->save();
		$this->assertObjectDatabaseValue( $a1, 'child', $d->id );
		

	}
	
	public function testAggrSet() {
		
		DomArOneTestClass::getTable()->update();
		
		$a1 = new DomArOneTestClass('a');
		$a2 = new DomArOneTestClass('a');
		$e = new DomArOneChildTestClass('e');
		$f = new DomArOneChildTestClass('f');
		$g = new DomArOneChildTestClass('g');
		
		
		// test replace inversing
		$a1->aggrChild = $e;
		$this->assertIdentical( $a1->aggrChild, $e );

		
		// test replacing aggreagtion saved property value
		
		$a1->save();
		$this->assertObjectDatabaseValue( $a1, 'aggrChild', $e->id );
		$this->assertObjectExist( $e );
		
		$a1->aggrChild  = $f;
		
		$this->assertIdentical( $a1->aggrChild, $f );
		$this->assertObjectDatabaseValue( $a1, 'aggrChild', NULL );
		$this->assertObjectExist( $e );
		$this->assertObjectExist( $a1 );
		
	
		$a1->save();
		$this->assertObjectDatabaseValue( $a1, 'aggrChild', $f->id );

		
	}
	
	public function testLoad() {
		DomArOneTestClass::getTable()->update();
		
		$a1 = new DomArOneTestClass('a');
		$b = new DomArOneChildTestClass('b');
		$c = new DomArOneChildTestClass('c');
		$d = new DomArOneChildTestClass('d');
		
		$a1->save();
		
		// test setting parent child to NULL if theres no child
		DomAr::flushStoredObjects();
		$this->assertIdentical( $a1->child, NULL );

		
		// preparing load tests
		$a1->child = $b;
		$a1->save();
		$this->assertObjectDatabaseValue( $a1, 'child', $b->id );
		
		// loading from parent side
		DomAr::flushStoredObjects();
		
		$aa1 = DomArOneTestClass::load( $a1->id );
		$aa1->name = 'aa';
		$this->assertNotIdentical( $aa1->name, $a1->name );
		
		$this->assertIdentical( $aa1->child->name, 'b' );
		
	}
	
	public function testValidate() {
		
		$m = new DomArOneTestClass('m');
		$a = new DomArOneChildTestClass('a');
		$b = new DomArOneChildExtTestClass('b');
		
		$p = $m->getProperty('child');
		
		$this->assertEqual( $p->validate( $m, $m ),1 );
		$this->assertEqual( $p->validate( $m, $a ),0 );
		$this->assertEqual( $p->validate( $m, $b ),0 );
		$n = 0;
		$this->assertEqual( $p->validate( $m,  $n ),0 );
		
	
	}
	
	public function testSerialize() {
		DomArOneTestClass::getTable()->update();
		
		$m = new DomArOneTestClass('m');
		$a = new DomArOneChildTestClass('a');
		$b = new DomArOneChildTestClass('b');
		
		$m->aggrChild = $a;
		$m->save();
		$m->child = $b;
		
		DomAr::flushStoredObjects();
		$result = DomArObject::unserialize( $m->serialize() );
		$result->name = 'nm';
		
		$this->assertNotEqual( $result->name, $m->name );
		$this->assertFalse( is_object($result->access('aggrChild')) );
		$this->assertIdentical( $result->access('aggrChild'), $a->id );
		$this->assertTrue( is_object($result->access('child')) );
		$this->assertIdentical( $result->access('child')->id, $b->id );
		

		// test if already saved child is changed. 
		$result->aggrChild->name = 'aa';

		DomAr::flushStoredObjects();
		$result2 = DomArObject::unserialize( $result->serialize()  );
		$result2->name = 'nm2';
		
		$this->assertNotEqual( $result2->name, $result->name );
		$this->assertIdentical( $result2->access('aggrChild')->id, $a->id );
		$this->assertIdentical( $result2->access('aggrChild')->name, 'aa' );
		$this->assertIdentical( $result2->access('child')->id, $b->id );
		
		
		// test if memory is not cleaned - serialized object already exists
		$result3 = DomArObject::unserialize( $result2->serialize()  );
		$result3->name = 'nm3';
		$this->assertEqual( $result3->name, $result2->name );
		
		$result3->aggrChild->delete();
		$result3->delete();
		
		
		$this->assertTableEmpty( $m->getTableName() );
		$this->assertTableEmpty( $a->getTableName() );
	}

}
?>